/**
  ******************************************************************************
  * @file    xxx.c 
  * @author  SD Electronics xxx
  * @version V1.0
  * @date    xx-xx-xx
  * @brief   xxxxxxxxxxxxxxxxxx.
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "sd_common.h"


/** 
  * @brief  ADC  
  */ 
ST_ADC_PHY g_stADCPhyParam = 
{
    ADC_TRANS_SOURCE_VBAT,
    ADC_REFERENCE_AVDD,
};


/** 
  * @brief ADC ????? 
  */ 
ST_ADC_CTRL g_stADCCtrl = 
{                       
    false,
    &g_stADCPhyParam,
};  

/**
  * @}
  */


/**
  * @brief  SDInitADC
  * @note   xxx
  * @param  None
  * @retval None
  */
void SDInitADC(PST_ADC_PHY pstADCCntPhy)
{
    if(ADC_TRANS_SOURCE_VBAT == pstADCCntPhy->ADC_Sample_typ)
    {
        struct adc_cfg_t cfg;
        memset((void*)&cfg, 0, sizeof(cfg));
        cfg.src = ADC_TRANS_SOURCE_VBAT;
        cfg.ref_sel = ADC_REFERENCE_INTERNAL;
        cfg.int_ref_cfg = ADC_INTERNAL_REF_1_2;
        cfg.clk_sel = ADC_SAMPLE_CLK_24M_DIV13;
        cfg.clk_div = 0x3f;
        adc_init(&cfg);
        adc_disable();
    }
    printf("usr: sd ADC init.\r\n");
}

/**
  * @brief  ADCOpen
  * @note   xxx
  * @param  None
  * @retval None
  */
static long ADCOpen(void *pvHandle, unsigned long ulOption)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;

    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        PST_ADC_CTRL pstADCCtrl = (PST_ADC_CTRL)pstDevManage->pvData;
        if(false == pstADCCtrl->bInitFlag)
        {
			//初始化
            SDInitADC(pstADCCtrl->pstADCInfo);
            pstADCCtrl->bInitFlag = true;
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;
}  

/**
  * @brief  ADCRead
  * @note   xxx
  * @param  None
  * @retval None
  */
static long ADCRead(void *pvHandle, unsigned long ulOpertion, unsigned char *pucDataBuf, int* ulDataSize)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        PST_ADC_CTRL pstADCCtrl = (PST_ADC_CTRL)pstDevManage->pvData;
		uint16_t result, ref_vol, vbat_vol;
        //
        if(true == pstADCCtrl->bInitFlag)
        {
            switch(ulOpertion)
            {
                case ADC_TRANS_SOURCE_VBAT:
                adc_enable(NULL, NULL, 0);
                adc_get_result(ADC_TRANS_SOURCE_VBAT, 0, &result);
                ref_vol = adc_get_ref_voltage(ADC_REFERENCE_INTERNAL);
                adc_disable();
                vbat_vol = (result * 4 * ref_vol) / 1024;
                
				pucDataBuf[0] = vbat_vol & 0x00FF;
				pucDataBuf[1] = ((vbat_vol & 0xFF00) >> 8);
                //printf("[BAT]=%d.\r\n", vbat_vol);
                break;
                case ADC_TRANS_SOURCE_PAD:
                    
                break;
                default:
                break;
            }
            
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;    
}

/**
  * @brief  ADCWrite
  * @note   xxx
  * @param  None
  * @retval None
  */
static long ADCWrite(void *pvHandle, unsigned long ulOpertion, unsigned char *pucDataBuf, unsigned long ulDataLen)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        PST_ADC_CTRL pstADCCtrl = (PST_ADC_CTRL)pstDevManage->pvData;
        //
        if(true == pstADCCtrl->bInitFlag)
        {
           ;
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;
}

/**
  * @brief  ADCControl
  * @note   xxx
  * @param  None
  * @retval None
  */
static long ADCControl (void *pvHandle,unsigned long ulOpertion, unsigned long ulControlCmd, void *pvd)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    { 
        PST_ADC_CTRL pstADCCtrl = (PST_ADC_CTRL)pstDevManage->pvData;
        /* ??????? */
        if(true == pstADCCtrl->bInitFlag)
        {
          ;
        }
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;             
}

/**
  * @brief  ADCClose
  * @note   xxx
  * @param  None
  * @retval None
  */
static long ADCClose (void *pvHandle)
{
    PST_DEV_MANAGE pstDevManage = pvHandle;
    long lRet = EN_DEV_CTL_RET_CODE_FAILED;
    if (0 != pstDevManage)
    {
        lRet = EN_DEV_CTL_RET_CODE_SUCCESSED;
    }
    return lRet;     
}



/**
  * @brief  g_stADCCtlFUN
  * @note  xxx
  * @param  None
  * @retval None
  */
ST_CTR_FUN g_stADCCtlFUN = 
{
    ADCOpen,		//??
    ADCRead,		//?
    ADCWrite,		//?
    ADCControl,		//??
    ADCClose    	//??
};


/******************* (C) COPYRIGHT 2011 ShenZhenShiding electronics **************************/
